package com.peternguyen.totolistpro.dao;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.peternguyen.totolistpro.data.Account;
import com.peternguyen.totolistpro.sql.TodoSQLiteHelper;

public class AccountDAO {
	private SQLiteDatabase db;
	private TodoSQLiteHelper dbHelper;

	public AccountDAO(Context context) {
		dbHelper = new TodoSQLiteHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	// Close the db
	public void close() {
		db.close();
	}

	// Create
	public void createAcount(String pwd, String email) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("pwd", pwd);
		contentValues.put("email", email);
		// Insert into DB
		db.insert("tblAccount", null, contentValues);
	}

	// update
	public void UpdatePWD(int id, String pwd, String email) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("pwd", pwd);
		contentValues.put("email", email);
		// Update datatbase
		db.update("tblAccount", contentValues, "_id= " + id, null);
	}

	// Delete
	public void deleteTodo(int todoId) {
		// Delete from DB where id match
		db.delete("todos", "_id = " + todoId, null);
	}

	// Get Acc
	
	public Account getAccount() {
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id","pwd", "email"};
		
		// Query the database
		Cursor cursor = db.query("tblAccount", tableColumns, null, null, null, null, null);
		cursor.moveToFirst();
		Account acc = new Account();
		// Iterate the results
	    while (!cursor.isAfterLast()) {	    	
	    	// Take values from the DB
	    	acc.set_id(cursor.getInt(0));
	    	acc.set_pwd(cursor.getString(1));
	    	acc.set_email(cursor.getString(2));
	    	cursor.moveToNext();
	    }
		
		return acc;
	}
	
	 
}
